package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.TbClueTrackRecordService;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 93238
* @description 针对表【tb_clue_track_record(线索跟进记录)】的数据库操作Service实现
* @createDate 2023-10-12 06:35:46
*/
@Service
public class TbClueTrackRecordServiceImpl extends ServiceImpl<TbClueTrackRecordMapper, TbClueTrackRecord>
    implements TbClueTrackRecordService{

    @Resource
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Resource
    private TbClueMapper tbClueMapper;
    @Override
    public List<TbClueTrackRecord> recordList(Long clueId) {
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.recordList(clueId);
        return list;
    }

    @Override
    public TbClueTrackRecord getRecordById(Integer id) {
        TbClueTrackRecord tbClueTrackRecord = tbClueTrackRecordMapper.getRecordById(id);
        return tbClueTrackRecord;
    }

    @Override
    public void addRecord(ClueTrackRecordVo clueTrackRecordVo) {
        //new一个实体储存线索跟进记录对象
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        //把前端数据拷贝到实体中
        BeanUtils.copyProperties(clueTrackRecordVo , tbClueTrackRecord);
        //设置创建时间
        tbClueTrackRecord.setCreateTime(DateUtils.getNowDate());
        //设置跟进人
        tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());
        //设置线索id

        TbClue tbClue=new TbClue();
        org.springframework.beans.BeanUtils.copyProperties(tbClueTrackRecord,tbClue);
        tbClue.setStatus("2");
        tbClue.setId(clueTrackRecordVo.getClueId());

        //MybatisPlus更新数据
        /*UpdateWrapper<TbClue> eq = new UpdateWrapper<TbClue>().eq("id", tbClue.getId());
        tbClueMapper.update(tbClue , eq);*/

        tbClueMapper.updateTbClue(tbClue);
        tbClueTrackRecord.setClueId(tbClue.getId().toString());

        //MybatisPlus的新增
        //tbClueTrackRecordMapper.insert(tbClueTrackRecord);
        tbClueTrackRecordMapper.addRecord(tbClueTrackRecord);
    }

    /*
    * 答案
    * */
    @Override
    public int insertTbClueTrackRecord(TbClue tbClue, TbClueTrackRecord tbClueTrackRecord) {
        tbClueMapper.updateTbClue(tbClue);
        tbClueTrackRecord.setClueId(tbClue.getId().toString());
        return tbClueTrackRecordMapper.addRecord(tbClueTrackRecord);
    }
}




